﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_Quan_ly_user : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetAllUser();
        }
    }
    protected void GetAllUser()
    {
        
        try {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetAllUser";
            DataTable db = Dal.GetDataTable();
            grvUser.DataSource = db;
            grvUser.DataBind();
        }
        catch { }
    }
    protected void grvUser_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grvUser.EditIndex = -1;
        GetAllUser();
    }
    protected void grvUser_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string i = Convert.ToString(grvUser.DataKeys[e.RowIndex].Value);
        //try
        //{
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_DeleteUser";
            Dal.ParameterCollection = new string[1] { "@UserID"};
            Dal.ValueCollection = new string[1] { i};
            Dal.ExecuteNonQuery();
            Dal.Close();
            Dal.Dispose();
            GetAllUser();
        //}
        //catch { }
    }
    protected void grvUser_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grvUser.EditIndex = e.NewEditIndex;
        GetAllUser();
    }

    protected void grvUser_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string i = Convert.ToString(grvUser.DataKeys[e.RowIndex].Value);
        bool cb = ((CheckBox)(grvUser.Rows[e.RowIndex].Cells[2].Controls[0])).Checked;
        bool cb1 = ((CheckBox)(grvUser.Rows[e.RowIndex].Cells[3].Controls[0])).Checked;
        try {
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_UpdateUsersByAdmin";
            Dal.ParameterCollection = new string[3] { "@UserID","@IsAdmin","@IsActive" };
            Dal.ValueCollection = new string[3] { i,cb.ToString(),cb1.ToString()};
            Dal.ExecuteNonQuery();
            Dal.Close();
            Dal.Dispose();
            grvUser.EditIndex = -1;
            GetAllUser();
        }
        catch { }
    }
   
}
